package pl.edu.pk.mech.kkbus.repository;

import static pl.edu.pk.mech.kkbus.staticstrings.BusStopProperty.*;
import static pl.edu.pk.mech.kkbus.staticstrings.NameEntity.NAME_ENTITY_BUS_STOP;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import pl.edu.pk.mech.kkbus.datastore.common.SynchronizeCommon;
import pl.edu.pk.mech.kkbus.model.BusStop;
import pl.edu.pk.mech.kkbus.model.BusStop.Direction;
import pl.edu.pk.mech.kkbus.model.Pojo;

import com.google.appengine.api.datastore.Entity;

public class BusStopDAO extends BaseDAO{
	  
//	przeslonic createOrUpdate
	
	  public List<BusStop> getAllBusStop(){
		  List<BusStop> temp = new ArrayList<BusStop>();
		  Iterable<Entity> entityList = SynchronizeCommon.listEntities(NAME_ENTITY_BUS_STOP, null, null);
		  for (Entity entity : entityList) {
			  temp.add(new BusStop(entity));
		  }
		  return temp;
	  }

	@Override
	public void fillProperty(Pojo pojo, Entity entity) {
		  BusStop busStop = (BusStop)pojo;

          Set<BusStop.Direction> direction = new HashSet<BusStop.Direction>();
          Set<Direction> directionTemp = busStop.getDirection();
          for (Iterator iterator = directionTemp.iterator(); iterator.hasNext();) {
        	  Direction dir = (Direction) iterator.next();
			  direction.add(dir);  
			}
          String dirTemp = direction.toString();
          
		  entity.setProperty(BUS_STOP_NAME, busStop.getName());
		  entity.setProperty(BUS_STOP_PRICE, busStop.getPrice());
		  entity.setProperty(BUS_STOP_DISTANCE, busStop.getDistance());
		  entity.setProperty(BUS_STOP_ORDER, busStop.getOrder());
		  entity.setProperty(BUS_STOP_ID_DEP, busStop.getDepartureTimes());
		  entity.setProperty(BUS_STOP_DIRECTION, dirTemp);
	}
}
